package com.lvwenguo.controller.sale;

import cn.itcast.commons.CommonUtils;
import com.lvwenguo.model.SaleData;
import com.lvwenguo.service.SaleInter;
import com.lvwenguo.service.serviceImple.SaleImple;
import com.lvwenguo.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//模拟销售
@WebServlet(urlPatterns = "/saleServlet")
public class saleServlet extends BaseServlet {
    SaleInter saleImpol =new SaleImple();
    //用于异步交互实现数据回显
    public void selectPaice(HttpServletRequest request,HttpServletResponse response){


        try {
            double i = saleImpol.selectPrice(request.getParameter("bar"));
            if(i==0){
                response.getWriter().println("");
            }else{
                response.getWriter().println(i);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用于添加信息
    public void Add(HttpServletRequest request,HttpServletResponse response){
        SaleData saleData = CommonUtils.toBean(request.getParameterMap(), SaleData.class);
       //查看是否有信息未输入
        if(saleData.getProduct_bar_code().equals("") && saleData.getSale_count()>0 && saleData.getSale_date().equals("") && saleData.getSale_price()<0){
            try {
                response.getWriter().print("检查信息是否完整");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else{
            //用户输入信息正确
            String error = saleImpol.AddSale(saleData, (String) request.getSession().getAttribute("sessionname"));
            try {
                response.getWriter().println(error);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
